const express = require('express');
const router = express.Router();
const db = require('@utils/dbConfig'); //连接mysq
const { CODE_ERROR, CODE_SUCCESS } = require("@/utils/constant");
const path = require('path')
const UUID = require('uuid');
const multer = require('multer'); //上传功能的插件（两种上传方式不一样）
const fs = require("fs");
const formidable = require('formidable'); //上传功能的插件（两种上传方式不一样）
const {

    SRC_URL
} = require('@utils/constant');
router.get('/marryInfo', function(req, res, next) {
    console.log("========", res.query)
    let { type } = req.query
    let latitude = 24.655693
    let longitude = 118.211606
    let theme = '婚礼邀请函'
    let weddingDate = '2025年05月10号'
    let address = '翔安区马巷镇赵厝社区居委会'
    let location = '赵厝社区居委会'
    let imgUrl = '请输入你的服务器地址'

    if (type == 1) {

        theme = '阿华喊你来干饭喽!'
        latitude = 24.677546 // 示例：北京的纬度
        longitude = 116.89121


        weddingDate = '2025年05月01号'
        address = '永定区望森楼东100米'
        location = '望森楼东100米'
    }
    res.json({
        code: CODE_SUCCESS,
        msg: '查询成功',
        data: {
            bridegroomTel: "15860708964",
            brideTel: "15280282900",
            address: address,
            location,
            mapName: '方良权和陈艳华的婚礼',
            maleName: '方良权',
            femaleNname: '陈艳华',
            latitude,
            longitude,
            weddingDate,
            theme,
            blessing: true,
            imageUrlFinally: imgUrl + '/img/m01.png',
            innerAudioSrc: imgUrl + "img/iMissYou.mp3",
            swiperItem1: {
                id: 1,
                img1: imgUrl + '/img/m02.png',
            },
            swiperItem2: {
                id: 2,
                img1: imgUrl + '/img/m03.png',
                img2: imgUrl + '/img/m04.png',
                img3: imgUrl + '/img/m05.png',
            },
            swiperItem3: {
                id: 3,
                img1: imgUrl + '/img/m06.png',
            },
            swiperItem4: {
                id: 4,
                img1: imgUrl + '/img/m07.png',
            },
            swiperItem5: {
                id: 5,
                img1: imgUrl + '/img/m08.png',
                img2: imgUrl + '/img/m09.png',
                img4: imgUrl + '/img/m11.png',
                img5: imgUrl + '/img/m12.png',
            },
            swiperItem6: {
                id: 6,
                img1: [
                    imgUrl + '/img/m13.png',
                    imgUrl + '/img/m14.png',
                    imgUrl + '/img/m15.png',
                    imgUrl + '/img/m16.png',
                    imgUrl + '/img/m17.png',
                    imgUrl + '/img/m18.png',
                    imgUrl + '/img/m19.png',
                ],
            },
        }
    });
});

router.get('/barragesList', function(req, res, next) {

    let { currentPage, pagersize } = req.query


    let sql = "select * from blessing order by create_time desc ";
    if (currentPage != -1) {
        sql = sql + `LIMIT ${(currentPage * 1 - 1) * pagersize}, ${pagersize  }`
    } else {
        sql = sql + ";"
    }
    console.log(sql, "sql");
    db.query(sql, (err, result) => {
        if (err) {
            return res.json({
                code: CODE_ERROR,
                msg: '查询失败',
                data: null
            });
        } else {
            db.query("select count(*) from blessing", (err, data1) => {
                if (err) {
                    return res.json({
                        code: CODE_ERROR,
                        msg: '查询失败',
                        data: null
                    });
                } else {
                    return res.json({
                        code: CODE_SUCCESS,
                        msg: '查询成功',
                        data: { list: result, total: data1[0]['count(*)'] }
                    });
                }

            })


        }
    })
});
router.post('/insetBarrages', function(req, res, next) {
    let { title, name, avatar, create_time } = req.body;
    let sql = `INSERT INTO blessing ( title, name, avatar,create_time) VALUES ("${title}","${name}","${avatar}","${create_time}");`
    db.query(sql, (err, result) => {
        if (err) {
            return res.json({
                code: CODE_ERROR,
                msg: err,
                data: null
            });
        }
        if (result.affectedRows === 0) {
            return res.json({
                code: CODE_ERROR,
                msg: "新增失败"
            })
        }
        res.json({
            code: CODE_SUCCESS,
            msg: '新增成功'
        })
    })
});





let storage = multer.diskStorage({
    destination: function(req, file, cb) {
        console.log(path.join(SRC_URL + '/../../web/dist/img/'), '1122')
        cb(null, path.join(SRC_URL + "/../../web/dist/img/"))
    },
    filename: function(req, file, cb) {
        try {
            let id = UUID.v1();
            let originalname = Buffer.from(file.originalname, "latin1").toString(
                "utf8"
            );

            cb(null, id + "_" + originalname)
        } catch (e) {
            cb(new Error('上传文件格式'), false);
        }
    },
})

function fileFilter(req, file, cb) {
    console.log(file.mimetype, 'file.mimetype')
    const allowedTypes = ['image/png', 'image/jpeg'];
    if (allowedTypes.includes(file.mimetype)) {
        cb(null, true); // 允许上传
    } else {
        cb(new Error('上传文件格式错误'), false); // 拒绝上传
    }
}
const upload = multer({
    storage: storage,
    fileFilter
}).single('file')

router.post('/wxImage', upload, (req, res) => {
    //上传文件只能通过这个插件接收  file是上传文件 fields是其他的

    res.json({
        code: CODE_SUCCESS,
        msg: '上传成功',
        data: req.file.filename
    });
})

module.exports = router